package com.zelin.controller;

import com.zelin.exception.MyException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

/**
 * ------------------------------
 * 功能：使用全局异常处理(版本二)
 * 作者：WF
 * 微信：hbxfwf13590332912
 * 创建时间：2021/11/15-10:05
 * ------------------------------
 */
@Controller
@RequestMapping("/user2")
public class UserController2 {

    //1. 用户登录
    @PostMapping("/login")
    public String login(String username, String password, Model model, HttpSession session){
        //1.1 如果用户名是admin,密码也是admin，就允许登录，否则，不允许,登录成功后，将用户放到session中
        if("admin".equals(username) && "admin".equals(password)){   //登录成功
            session.setAttribute("username",username);
            //model.addAttribute("username",username);
            return "welcome";
        }else{                                                       //登录不成功
            throw new MyException("对不起，用户名或密码不对！[全局异常]");

        }
        //return "redirect:/login.jsp";
    }
}
